package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.gson.Gson;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;
@Service(interfaceClass = Redis4SetMealCacheService.class)
public class Redis4SetMealCacheServiceImpl implements Redis4SetMealCacheService {
    @Autowired
    private JedisPool jedisPool;
    /*
     *将所有套餐信息存进redis作为缓存
     */
    @Override
    public void pushAllSetMeal2Cache(List<Setmeal> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        jedisPool.getResource().set("setmeallist", json);
    }
    /*
     *将指定套餐信息存进redis作为缓存
     */
    @Override
    public void pushSetMeal2Cache(Setmeal setmeal) {
        Gson gson = new Gson();
        String json = gson.toJson(setmeal);
        jedisPool.getResource().set("setmeal" + setmeal.getId(), json);
    }

    @Override
    public void clearAllSetMealCache() {
        jedisPool.getResource().del("setmeallist");
    }

    @Override
    public void clearSetMealCache(Integer id) {
        jedisPool.getResource().del("setmeal"+id);
    }
}
